import 'package:bot_toast/bot_toast.dart';
// import 'package:euromillions/tools/EJ_config.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
// import 'package:kerala_lottery/tools/KL_Generator.dart';
// import 'package:kerala_lottery/tools/models/KL_keralaLottoDataModel.dart';

import '../../tools/KL_Generator.dart';
import '../../tools/KaL_Config.dart';
import '../../tools/FL_CustomLoading.dart';
import '../../tools/FL_NumberGenerator.dart';
import '../../tools/EJ_ballsView.dart';
import '../../tools/FL_emsBalls.dart';
import '../../tools/KaL_drawNumbersView.dart';
import '../../tools/models/KL_keralaLottoDataModel.dart';
import '../../tools/models/KL_keralaLottoModel.dart';

class EJGenerator_HomePage extends StatefulWidget {
  EJGenerator_HomePage({super.key, this.pageController});
  PageController? pageController;

  @override
  State<EJGenerator_HomePage> createState() => _EJGenerator_HomePageState();
}

class _EJGenerator_HomePageState extends State<EJGenerator_HomePage> {
  NextDrawModel? _nextDrawnModel;
  var _numbersMap = {};

  ///
  List<Widget> balls = [];
  List<Map> saveNumbers = [];

  late int scoredForNumbers = 0; //号码分析打分结果

  ///
  _goToGeneratorPage() {
    // HL_JKLocalStorage.setStringList(NL_config.NL_GeneratorNumbers, saveNumbers);
    // HL_JKLocalStorage.setBool(NL_config.NL_haveGeneratorNumbers, true);

    var Page = widget.pageController!;
    Page.jumpToPage(3);
  }

  //随机生成号码、并打分
  _getgenerateRandomNumbers() async {
    BotToast.showCustomLoading(toastBuilder: (cancelFunc) {
      return FLCustomLoading();
    });

    balls.clear();
    saveNumbers.clear();

    await FLNumberGenerator.instance.generateNumbers();
    saveNumbers = FLNumberGenerator.instance.saveNumbers;
    // var generaNumbers = FLNumberGenerator.instance.saveNumbers;

    // for (var i = 0; i < saveNumbers.length; i++) {
    //   var element = saveNumbers[i];
    //   if (i < saveNumbers.length - 1) {
    //     balls.add(ball(element, true));
    //   } else {
    //     balls.add(ball(element, false));
    //   }
    // }
    scoredForNumbers = FLNumberGenerator.instance.scoredForNumbers;

    Future.delayed(const Duration(seconds: 1), () {
      BotToast.closeAllLoading();
    });
    setState(() {});
  }

  _generateRandomNumbers() async {
    ///
    _nextDrawnModel = KL_keralaLottoDataModel().nextDrawnModel;
    String randomNumbers = await KL_Generator.instance
        .generateNumbers(_nextDrawnModel!.ticket_prefix!);

    List balls = [];
    List<String> charArray = randomNumbers.split('');
    // numbersMap = charArray;
    for (var i = 0; i < charArray.length; i++) {
      var ball = {
        'number': charArray[i],
        'isRight': false,
        'isNormal': true
      }; //isNormal代表是否对比中奖号码，true不用对比，false需要对比。isRight代表是否中奖号码
      balls.add(ball);
    }
    _numbersMap = {'title': '', 'balls': balls};

    scoredForNumbers = KL_Generator.instance.scoredForNumbers;

    setState(() {});
  }

  @override
  void initState() {
    super.initState();

    _getgenerateRandomNumbers();

    _generateRandomNumbers();
  }

  @override
  Widget build(BuildContext context) {
    //进度条比例
    double progress = scoredForNumbers / 100;
    return GestureDetector(
      onTap: () {
        debugPrint('go to generator');
        _goToGeneratorPage();
      },
      child: Container(
        height: 160,
        margin: EdgeInsets.all(10),
        decoration: BoxDecoration(
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.2), // 阴影颜色
              spreadRadius: 1, // 扩散半径
              blurRadius: 10, // 模糊半径
              offset: Offset(0, 1), // 阴影偏移
            ),
          ],
          borderRadius: BorderRadius.all(Radius.circular(10)),
          color: Colors.white,
        ),
        child: Stack(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 110),
              child: Container(
                height: 50,
                // width: 300,
                decoration: const BoxDecoration(
                  // color: HexColor('#f6f6f6'),
                  borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(10),
                      bottomRight: Radius.circular(10)),
                ),
                child: Stack(
                  children: [
                    SizedBox(
                      // color: Colors.white,
                      height: 50,
                      child: LinearProgressIndicator(
                        borderRadius: const BorderRadius.only(
                            bottomLeft: Radius.circular(10),
                            bottomRight: Radius.circular(10)),
                        backgroundColor: HexColor('##F6F6F6'), // 设置进度条背景颜色
                        valueColor: AlwaysStoppedAnimation<Color>(
                            HexColor('##C9FFCF')), // 设置进度条颜色
                        value: progress, // 设置进度值（0.0 - 1.0）
                      ),
                    ),
                    SizedBox(
                      height: 50,
                      child: Padding(
                        padding: const EdgeInsets.only(top: 10),
                        child: Row(
                          children: [
                            const SizedBox(width: 15),
                            const Text('AI scored your numbers:',
                                style: TextStyle(
                                  fontSize: 13,
                                  fontWeight: FontWeight.w300,
                                )),
                            const Spacer(),
                            Text(
                              scoredForNumbers.toString(),
                              style: TextStyle(
                                  fontWeight: FontWeight.w500,
                                  fontSize: 13,
                                  color: HexColor('#2BA471')),
                            ),
                            const SizedBox(width: 20),
                            Image.asset('assets/sys_imgs/EJ_union.png',
                                width: 8.0, height: 13.0),
                            const SizedBox(width: 15),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Container(
              height: 120,
              width: double.infinity,
              // padding: EdgeInsets.only(left: 10, right: 10),
              decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(10),
                  )),
              child: Column(
                children: [
                  Container(
                    // color: Colors.yellow,
                    margin: const EdgeInsets.only(left: 10, right: 10, top: 10),
                    height: 30,
                    child: Row(
                      children: [
                        const Text('Generator & Analytics',
                            style: TextStyle(
                              fontWeight: FontWeight.w600,
                              fontSize: 13,
                            )),
                        const Spacer(),
                        Container(
                          height: 35,
                          width: 80,
                          // alignment: Alignment.center,
                          decoration: BoxDecoration(
                              color: KaL_Config.KaL_MainColor,
                              borderRadius: const BorderRadius.all(
                                  Radius.circular(35 * 0.5))),
                          child: Center(
                            child: TextButton(
                                onPressed: () {
                                  debugPrint('Generate');
                                  // _getgenerateRandomNumbers();
                                  _generateRandomNumbers();
                                },
                                child: const Text(
                                  'Generate',
                                  style: TextStyle(
                                    fontSize: 11,
                                    fontWeight: FontWeight.w400,
                                    color: Colors.white,
                                  ),
                                )),
                          ),
                        )
                      ],
                    ),
                  ),
                  KalDrawnumbersview(data: _numbersMap),
                  // Container(
                  //   alignment: Alignment.center,
                  //   margin: const EdgeInsets.only(left: 10, right: 10),
                  //   height: 60,
                  //   width: double.infinity,
                  //   decoration: BoxDecoration(
                  //     color: HexColor('#eeeeee'),
                  //     borderRadius:
                  //         const BorderRadius.all(Radius.circular(8)),
                  //   ),
                  //   // child: EJBallsView(numbers: saveNumbers),
                  //   // child: FLBallsView(balls: saveNumbers),
                  //   child: StrokeText(
                  //     text: 'PW 649722',
                  //     textStyle: TextStyle(
                  //       fontSize: 35,
                  //       color: KaL_Config.AL_MainColor,
                  //       fontWeight: FontWeight.w800,
                  //     ),
                  //     strokeColor: KaL_Config.AL_jackpottextColor_w,
                  //     strokeWidth: 5,
                  //   ),
                  // )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
